package com.education.music.activity;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.education.music.R;
import com.education.music.util.CommSharedUtil;
import com.education.music.weight.DialogLoadding;

public class UserLoginActivity extends AppCompatActivity {

    private EditText user_phone;
    private EditText user_pwd;
    private TextView login;
    private DialogLoadding mLoadding;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);
        initView();
    }

    private void initView() {
        mLoadding = new DialogLoadding(this);
        user_phone = findViewById(R.id.user_phone);
        user_pwd = findViewById(R.id.user_pwd);
        login = findViewById(R.id.login);
        login.setOnClickListener(v -> {
            String phoneNum = user_phone.getText().toString();
            String passWord = user_pwd.getText().toString();
            if (TextUtils.isEmpty(phoneNum)) {
                Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(passWord)) {
                Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            //TODO LOGIN
            mLoadding.showDialog();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mLoadding.closeDialog();
                        if (phoneNum.equals("13999999999") && passWord.equals("123456")) {
                        Toast.makeText(UserLoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        CommSharedUtil.getInstance(UserLoginActivity.this).putBoolean("login", true);
                        CommSharedUtil.getInstance(UserLoginActivity.this).putString("phone", "13999999999");
                        CommSharedUtil.getInstance(UserLoginActivity.this).putString("pwd", "123456");
                        finish();
                    } else if (phoneNum.equals("13981223344") && passWord.equals("123456")) {
                        Toast.makeText(UserLoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        CommSharedUtil.getInstance(UserLoginActivity.this).putBoolean("login", true);
                        CommSharedUtil.getInstance(UserLoginActivity.this).putString("phone", "13999999999");
                        CommSharedUtil.getInstance(UserLoginActivity.this).putString("pwd", "123456");
                        finish();
                    } else if (phoneNum.equals("16601330207") && passWord.equals("123456")) {
                        Toast.makeText(UserLoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        CommSharedUtil.getInstance(UserLoginActivity.this).putBoolean("login", true);
                        CommSharedUtil.getInstance(UserLoginActivity.this).putString("phone", "13999999999");
                        CommSharedUtil.getInstance(UserLoginActivity.this).putString("pwd", "123456");
                        finish();
                    } else {
                        Toast.makeText(UserLoginActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                    }
                }
            }, 3000);
        });
    }
}
